from rest_framework import serializers


from goods.models import SKU



class CartsCreateSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField(label="商品ID", min_value=1)
    count = serializers.IntegerField(label="商品数量", min_value=1)
    selected = serializers.BooleanField(label="是否勾选", default=True)

    def validate_sku_id(self, value):
        """
        验证sku_id对应的商品是否存在
        :param value: sku_id
        :return:
        """
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError("无法获取此sku_id")
        return value


class CartSKUSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField(label="商品数量", )
    selected = serializers.BooleanField(label="是否勾选")
    class Meta:
        model = SKU
        fields = ["id", "count", "name", "default_image_url", "price", "selected"]

